package com.wwg.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description 主要用于非空校验和变量比较的工具类
 * @systemName 融资平台
 * @author yyh
 * @date 2019年4月1日 下午4:21:34
 * @version V1.0
 */
public final class WWGAssertUtil {
	private static final Logger LOG = LoggerFactory.getLogger(WWGAssertUtil.class);

	private static Boolean equals(String arg0, String arg1) {
		return Boolean.valueOf(StringUtils.equals(arg0, arg1));
	}

	private static Boolean equals(Long arg0, Long arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(Long.compare(arg0.longValue(), arg1.longValue()) == 0);
		}
		return Boolean.FALSE;
	}

	private static Boolean equals(Integer arg0, Integer arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(Integer.compare(arg0.intValue(), arg1.intValue()) == 0);
		}
		return Boolean.FALSE;

	}

	private static Boolean equals(Short arg0, Short arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(Short.compare(arg0.shortValue(), arg1.shortValue()) == 0);
		}
		return Boolean.FALSE;
	}

	private static Boolean equals(Character arg0, Character arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return arg0.equals(arg1);
		}
		return Boolean.FALSE;
	}

	private static Boolean equals(Byte arg0, Byte arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(Byte.compare(arg0.byteValue(), arg1.byteValue()) == 0);
		}
		return Boolean.FALSE;
	}

	private static Boolean equals(Float arg0, Float arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(Float.compare(arg0.floatValue(), arg1.floatValue()) == 0);
		}
		return Boolean.FALSE;
	}

	private static Boolean equals(Double arg0, Double arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(Double.compare(arg0.doubleValue(), arg1.doubleValue()) == 0);
		}
		return Boolean.FALSE;
	}

	private static Boolean equals(BigDecimal arg0, BigDecimal arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 != null && arg1 != null) {
			return Boolean.valueOf(arg0.compareTo(arg1) == 0);
		}
		return Boolean.FALSE;
	}

	private static Boolean execute(Object arg0, Object arg1) {
		if (arg0 == null && arg1 == null) {
			return Boolean.TRUE;
		}
		if (arg0 == null || arg1 == null) {
			return Boolean.FALSE;
		}
		if (!arg0.getClass().equals(arg1.getClass())) {
			throw error("class type different");
		}
		if ((arg0 instanceof String)) {
			return equals(arg0.toString(), arg1.toString());
		}
		if ((arg0 instanceof Character)) {
			return equals((Character) arg0, (Character) arg1);
		}
		if ((arg0 instanceof Byte)) {
			return equals((Byte) arg0, (Byte) arg1);
		}
		if ((arg0 instanceof Short)) {
			return equals((Short) arg0, (Short) arg1);
		}
		if ((arg0 instanceof Integer)) {
			return equals((Integer) arg0, (Integer) arg1);
		}
		if ((arg0 instanceof Long)) {
			return equals((Long) arg0, (Long) arg1);
		}
		if ((arg0 instanceof Float)) {
			return equals((Float) arg0, (Float) arg1);
		}
		if ((arg0 instanceof Double)) {
			return equals((Double) arg0, (Double) arg1);
		}
		if ((arg0 instanceof BigDecimal)) {
			return equals((BigDecimal) arg0, (BigDecimal) arg1);
		}
		if (arg0.getClass().isEnum()) {
			return equals(arg0.toString(), arg1.toString());
		}
		return arg0.equals(arg1);
	}

	private static RuntimeException error(String msg) {
		LOG.error(msg);
		return new RuntimeException(msg);
	}

	@Deprecated
	public static Boolean checkParam(Object arg) {
		return execute(arg);
	}

	private static Boolean execute(Object arg) {
		if (arg == null) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof String)) && (StringUtils.isBlank(arg.toString()))) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Map)) && (((Map) arg).isEmpty())) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Byte)) && (Byte.compare(((Byte) arg).byteValue(), (byte) 0) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Short)) && (Short.compare(((Short) arg).shortValue(), (short) 0) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Integer)) && (Integer.compare(((Integer) arg).intValue(), 0) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Long)) && (Long.compare(((Long) arg).longValue(), 0L) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Float)) && (Float.compare(((Float) arg).floatValue(), 0.0f) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Double)) && (Double.compare(((Double) arg).doubleValue(), 0.0d) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof List)) && (((List) arg).isEmpty())) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof Set)) && (((Set) arg).isEmpty())) {
			return Boolean.valueOf(false);
		}
		if (((arg instanceof BigDecimal)) && (((BigDecimal) arg).compareTo(BigDecimal.ZERO) <= 0)) {
			return Boolean.valueOf(false);
		}
		if (arg.getClass().isArray()) {
			Object[] arg1 = (Object[]) arg;
			if(arg1.length <= 0){
				return Boolean.valueOf(false);
			}
			
			List list = new ArrayList();
			for (int i = 0; i < arg1.length; i++) {
				if(execute(arg1[i])){
					list.add(arg1[i]);
				}
			}
			
			if (list.isEmpty()) {
				return Boolean.valueOf(false);
			}
		}
		return Boolean.valueOf(true);
	}
	
	public static Boolean isEmpty(Object arg) {
		return !execute(arg);
	}
	
	public static Boolean equals(Object arg0, Object arg) {
		if (execute(arg0, arg).booleanValue()) {
			return Boolean.valueOf(true);
		}
		return Boolean.valueOf(false);
	}
	
}
